﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
        {
            DoPostBackAction();
        }

        if (dtlist.Items.Count == 0)
        {
            ViewHotProducts();
            ViewNewProducts();
            panelLoaiSanPham.Visible = false;   
        }
        else
            panelLoaiSanPham.Visible = true;

        string mlsp = Request.Params["MLSP"];
        if (mlsp == "")
            panelLoaiSanPham.Visible = false;
    }

    private void HideAllPanel()
    {
    }

    private void DoPostBackAction()
    {
        string actionCode = Request.Form.Get("__EVENTTARGET");  // or string actionCode = Request.Params.Get("__EVENTTARGET");
        string arguments = Request.Form.Get("__EVENTARGUMENT");

        switch (actionCode)
        {

        };
    }

    private void ViewHotProducts()
    {
        string qSelect = "Select TOP 9 a.MaSanPham,a.TenSanPham,a.NgayHetHan,a.GiaHienTai,a.HinhAnh,a.MaLoaiSanPham,a.MaTaiKhoan,c.TenTaiKhoan ,count (*) as \"n\"  from SANPHAM a, CHITIETDAUGIA b, TAIKHOAN c where a.MaSanPham=b.MaSanPham and a.MaTaiKhoan=c.MaTaiKhoan and DATEDIFF(day, NgayHetHan, GetDate()) <= 0 group by a.MaSanPham,a.TenSanPham,a.NgayHetHan,a.GiaHienTai,a.HinhAnh,a.MaLoaiSanPham,a.MaTaiKhoan,c.TenTaiKhoan order by n desc";
        SqlCommand cmd = new SqlCommand(qSelect, Helper.ConnectToDataBase());
        try
        {
            rptHotProducts.DataSource = cmd.ExecuteReader();
            rptHotProducts.DataBind();
            Helper.DisposeCommand(ref cmd);
        }
        catch (Exception)
        {
            Helper.DisposeCommand(ref cmd);
        }
        panelHotProduct.Visible = true;
    }

    private void ViewNewProducts()
    {
        string qSelect = "Select TOP 9 a.MaSanPham,a.TenSanPham,a.NgayHetHan,a.GiaHienTai,a.HinhAnh,a.MaLoaiSanPham,a.MaTaiKhoan,b.TenTaiKhoan from SANPHAM a, TAIKHOAN b where DATEDIFF(day, NgayHetHan, GetDate()) <= 0 and a.MaTaiKhoan=b.MaTaiKhoan order by NgayDang desc";
        SqlCommand cmd = new SqlCommand(qSelect, Helper.ConnectToDataBase());
        try
        {
            RepeaterNewProducts.DataSource = cmd.ExecuteReader();
            RepeaterNewProducts.DataBind();
            Helper.DisposeCommand(ref cmd);
        }
        catch (Exception ex)
        {
            Helper.DisposeCommand(ref cmd);
        }
        panelDauGiaNhieuNhat.Visible = true;
    }

    protected void UserLogout(object sender, EventArgs args)
    {
        Response.Cookies.Remove("daugia_user");
    }

}
